Micron Document
Livres et Wikis | Archives | Info


JavaScript syntax
part 24/30 Β· 107.0 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Objects

For convenience, types are normally subdivided into primitives and
objects. Objects are entities that have an identity (they are only equal
to themselves) and that map property names to values ("slots" in
prototype-based programming terminology). Objects may be thought of as
associative arrays or hashes, and are often implemented using these data
structures. However, objects have additional features, such as a
prototype chain, which ordinary associative arrays do not have.

JavaScript has several kinds of built-in objects, namely Array, Boolean,
Date, Function, Math, Number, Object, RegExp and String. Other objects
are "host objects", defined not by the language, but by the runtime
environment. For example, in a browser, typical host objects belong to
the DOM (window, form, links, etc.).

Creating objects

Objects can be created using a constructor or an object literal. The
constructor can use either a built-in Object function or a custom
function. It is a convention that constructor functions are given a name
that starts with a capital letter:

// Constructor
const anObject = new Object();
// Object literal
const objectA = {};
const objectA2 = {}; // AΒ != A2, {}s create new objects as copies.
const objectB = {index1: 'value 1', index2: 'value 2'};
// Custom constructor (see below)

Object literals and array literals allow one to easily create flexible
data structures:

const myStructure = {
name: {
first: "Mel",
last: "Smith"
},
age: 33,
hobbies: ["chess", "jogging"]
};

This is the basis for JSON, which is a simple notation that uses
JavaScript-like syntax for data exchange.

Methods

A method is simply a function that has been assigned to a property name
of an object. Unlike many object-oriented languages, there is no
distinction between a function definition and a method definition in
object-related JavaScript. Rather, the distinction occurs during
function calling; a function can be called as a method.

When called as a method, the standard local variable this is just
automatically set to the object instance to the left of the ".". (There
are also call and apply methods that can set this explicitlyβ€”some
packages such as jQuery do unusual things with this.)

In the example below, Foo is being used as a constructor. There is
nothing special about a constructor - it is just a plain function that
initializes an object. When used with the new keyword, as is the norm,
this is set to a newly created blank object.

Note that in the example below, Foo is simply assigning values to slots,
some of which are functions. Thus it can assign different functions to
different instances. There is no prototyping in this example.

function px() { return this.prefix + "X"; }
function Foo(yz) {
this.prefix = "a-";
if (yz > 0) {
this.pyz = function() { return this.prefix + "Y"; };
} else {
this.pyz = function() { return this.prefix + "Z"; };

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────